package com.liangzhong.modules.fameHall.mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.FameHallEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.ModuleMediator;
	import com.liangzhong.proxyconstant.fameHall.FameHallProxyConstant;
	import com.lz.football.module.base.IModuleView;
	import com.lz.football.module.fameHall.IFameHall;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	
	import org.puremvc.as3.interfaces.INotification;
	
	public class FameHallMediator extends ModuleMediator
	{
		public static const MediatorName :String = "FameHallMediator";
		
		/**
		 *是否放置球员 中
		 * true则，放置球员 ，不可以再放置
		 * false则可以放置球员
		 */
		private var _isPutPlayer:Boolean = false;
		public function FameHallMediator(mediatorName:String=null, viewComponent:Object=null, param:Object=null)
		{
			super(mediatorName, viewComponent, param);
		}
		
		/**
		 *获取 实例 
		 * @return 
		 * 
		 */		
		public function get clubView():DisplayObjectContainer
		{
			return viewComponent as DisplayObjectContainer;
		}
		/**
		 *添加事件 
		 * 
		 */		
		override public function addEventListeners():void
		{
			clubView.addEventListener(ModuleEvent.REMOVE_MODULE,onRemoveModuleHandler);
			clubView.addEventListener(FameHallEvent.PUTPLAYER,onPutPlayerHandler);
			clubView.addEventListener(FameHallEvent.GETAWARD,ongetwardHandler);
		}
		
		protected function ongetwardHandler(event:FameHallEvent):void
		{
			// TODO Auto-generated method stub
			this.sendNotification(FameHallProxyConstant.GETAWARD,event.param);
		}
		
		protected function onPutPlayerHandler(event:FameHallEvent):void
		{
			// TODO Auto-generated method stub
			if(_isPutPlayer)return;
			_isPutPlayer = true;
			this.sendNotification(FameHallProxyConstant.PUTPLAYER,event.param);
		}
		
		protected function onRemoveModuleHandler(event:Event):void
		{
			// TODO Auto-generated method stub
			removeMediator();
		}
		/**
		 * 删除事件 
		 * 
		 */
		override public function removeEventListeners():void
		{
			
		}
		/**
		 * 监听信息
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [FameHallProxyConstant.GET_FAMEHALLINFO,
				FameHallProxyConstant.GET_PUTPLAYER,
				FameHallProxyConstant.GET_GETAWARD]
		}
		/**
		 * 处理信息
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case FameHallProxyConstant.GET_FAMEHALLINFO:
					showUI();
					break;
				case FameHallProxyConstant.GET_PUTPLAYER:
					if(notification.getBody() == null)
					{
						_isPutPlayer = false;
						return;
					}
					(viewComponent as IFameHall).getPutPlayer(notification.getBody());
					_isPutPlayer = false;
					break;
				case FameHallProxyConstant.GET_GETAWARD:
					(viewComponent as IFameHall).getAward(notification.getBody());
					break;
				
			}
		}
		/**
		 * 
		 * 注册
		 */		
		override public function onRegister():void
		{
			resName = ResourceConst.FAMEHALL_SWF;
			if(ResourceManager.instance.isContainResource(ResourceConst.FAMEHALL_SWF))
			{
				showUI();
			}
			else
			{
				loader = new QueueLoader();
				var clubConfig :XML = XML(ResourceManager.instance.getResource(ResourceConst.MODULE_RES_CONFIG)).fameHall[0];
				bytesTotal = int(clubConfig.@totalBytes);
				var releaseVersion :String;
				for each(var item :XML in clubConfig.item)
				{
					releaseVersion = "?" + item.@version + "." + item.@itemType;
					loader.addQueue(new QueueVo(item.@resName,item.@id,item.@id),item.@url + releaseVersion);
				}
				loadRes();
			}
		}
		/**
		 *加载资源完毕 
		 * @param e
		 * 
		 */		
		override protected function onLoadComplete(e:QueueLoaderEvent):void
		{
			
			super.onLoadComplete(e);
			ResourceManager.instance.putResource(ResourceConst.FAMEHALL_SWF,resList);
//			showUI();
			
			this.sendNotification(FameHallProxyConstant.FAMEHALLINFO);
		}
		private function showUI():void
		{
			var cView :IModuleView = Reflection.createInstance("com.lz.football.module.fameHall.FameHallView");
			cView.initUI();
			viewComponent = cView;
			GlobalDef.GameWindowContainer.addChild(viewComponent as DisplayObject);
			addEventListeners();
		}
		/**
		 * 删除 mediator
		 * 
		 */		
		override public function removeMediator():void
		{
			facade.removeMediator(MediatorName);
		}
		/**
		 *删除 清理 
		 * 
		 */		
		override public function onRemove():void
		{
			super.onRemove();
			removeEventListeners();
			(viewComponent as IModuleView).destroyView();
			viewComponent = null;
		}
	}
}